home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 2003 February / macformat-126-disc-2.iso / pc / Resources (classic) / Mozilla 1.1 / Installer Modules / venkman.xpi / viewer / Components / venkman-service.js
Encoding:
Text File  |  2002-08-26  |  13.0 KB  |  449 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2.  *
  3.  * The contents of this file are subject to the Mozilla Public License
  4.  * Version 1.1 (the "License"); you may not use this file except in
  5.  * compliance with the License. You may obtain a copy of the License at
  6.  * http://www.mozilla.org/MPL/ 
  7.  * 
  8.  * Software distributed under the License is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  10.  * for the specific language governing rights and limitations under the
  11.  * License. 
  12.  *
  13.  * The Original Code is The JavaScript Debugger
  14.  * 
  15.  * The Initial Developer of the Original Code is
  16.  * Netscape Communications Corporation
  17.  * Portions created by Netscape are
  18.  * Copyright (C) 1998 Netscape Communications Corporation.
  19.  *
  20.  * Alternatively, the contents of this file may be used under the
  21.  * terms of the GNU Public License (the "GPL"), in which case the
  22.  * provisions of the GPL are applicable instead of those above.
  23.  * If you wish to allow use of your version of this file only
  24.  * under the terms of the GPL and not to allow others to use your
  25.  * version of this file under the MPL, indicate your decision by
  26.  * deleting the provisions above and replace them with the notice
  27.  * and other provisions required by the GPL.  If you do not delete
  28.  * the provisions above, a recipient may use your version of this
  29.  * file under either the MPL or the GPL.
  30.  *
  31.  * Contributor(s):
  32.  *  Robert Ginda, <rginda@netscape.com>, original author
  33.  *
  34.  */
  35.  
  36. /* components defined in this file */
  37. const CLINE_SERVICE_CTRID =
  38.     "@mozilla.org/commandlinehandler/general-startup;1?type=venkman";
  39. const CLINE_SERVICE_CID =
  40.     Components.ID("{18269616-1dd2-11b2-afa8-b612439bda27}");
  41. const JSDPROT_HANDLER_CTRID =
  42.     "@mozilla.org/network/protocol;1?name=x-jsd";
  43. const JSDPROT_HANDLER_CID =
  44.     Components.ID("{12ec790d-304e-4525-89a9-3e723d489d14}");
  45.  
  46. /* components used by this file */
  47. const CATMAN_CTRID = "@mozilla.org/categorymanager;1";
  48. const STRING_STREAM_CTRID = "@mozilla.org/io/string-input-stream;1";
  49. const MEDIATOR_CTRID =
  50.     "@mozilla.org/appshell/window-mediator;1";
  51. const SIMPLEURI_CTRID = "@mozilla.org/network/simple-uri;1";
  52.  
  53. const nsIWindowMediator    = Components.interfaces.nsIWindowMediator;
  54. const nsICmdLineHandler    = Components.interfaces.nsICmdLineHandler;
  55. const nsICategoryManager   = Components.interfaces.nsICategoryManager;
  56. const nsIProtocolHandler   = Components.interfaces.nsIProtocolHandler;
  57. const nsIURI               = Components.interfaces.nsIURI;
  58. const nsIURL               = Components.interfaces.nsIURL;
  59. const nsIStringInputStream = Components.interfaces.nsIStringInputStream;
  60. const nsIChannel           = Components.interfaces.nsIChannel;
  61. const nsIRequest           = Components.interfaces.nsIRequest;
  62. const nsIProgressEventSink = Components.interfaces.nsIProgressEventSink;
  63. const nsISupports          = Components.interfaces.nsISupports;
  64.  
  65. function findDebuggerWindow ()
  66. {
  67.     var windowManager =
  68.         Components.classes[MEDIATOR_CTRID].getService(nsIWindowMediator);
  69.  
  70.     var window = windowManager.getMostRecentWindow("mozapp:venkman");
  71.  
  72.     return window;
  73. }
  74.  
  75. /* Command Line handler service */
  76. function CLineService()
  77. {}
  78.  
  79. CLineService.prototype.commandLineArgument = "-venkman";
  80. CLineService.prototype.prefNameForStartup = "general.startup.venkman";
  81. CLineService.prototype.chromeUrlForTask = "chrome://venkman/content";
  82. CLineService.prototype.helpText = "Start with JavaScript Debugger.";
  83. CLineService.prototype.handlesArgs = false;
  84. CLineService.prototype.defaultArgs = "";
  85. CLineService.prototype.openWindowWithArgs = false;
  86.  
  87. /* factory for command line handler service (CLineService) */
  88. var CLineFactory = new Object();
  89.  
  90. CLineFactory.createInstance =
  91. function clf_create (outer, iid) {
  92.     if (outer != null)
  93.         throw Components.results.NS_ERROR_NO_AGGREGATION;
  94.  
  95.     if (!iid.equals(nsICmdLineHandler) && !iid.equals(nsISupports))
  96.         throw Components.results.NS_ERROR_INVALID_ARG;
  97.  
  98.     return new CLineService();
  99. }
  100.  
  101. /* x-jsd: protocol handler */
  102.  
  103. const JSD_DEFAULT_PORT = 2206; /* Dana's apartment number. */
  104.  
  105. /* protocol handler factory object */
  106. var JSDProtocolHandlerFactory = new Object();
  107.  
  108. JSDProtocolHandlerFactory.createInstance =
  109. function jsdhf_create (outer, iid) {
  110.     if (outer != null)
  111.         throw Components.results.NS_ERROR_NO_AGGREGATION;
  112.  
  113.     if (!iid.equals(nsIProtocolHandler) && !iid.equals(nsISupports))
  114.         throw Components.results.NS_ERROR_INVALID_ARG;
  115.  
  116.     return new JSDProtocolHandler();
  117. }
  118.  
  119. function JSDURI (spec, charset)
  120. {
  121.     this.spec = this.prePath = spec;
  122.     this.charset = this.originCharset = charset;
  123. }
  124.  
  125. JSDURI.prototype.QueryInterface =
  126. function jsdch_qi (iid)
  127. {
  128.     if (!iid.equals(nsIURI) && !iid.equals(nsIURL) &&
  129.         !iid.equals(nsISupports))
  130.         throw Components.results.NS_ERROR_NO_INTERFACE;
  131.  
  132.     return this;
  133. }
  134.  
  135. JSDURI.prototype.scheme = "x-jsd";
  136.  
  137. JSDURI.prototype.fileBaseName =
  138. JSDURI.prototype.fileExtension =
  139. JSDURI.prototype.filePath  =
  140. JSDURI.prototype.param     =
  141. JSDURI.prototype.query     =
  142. JSDURI.prototype.ref       =
  143. JSDURI.prototype.directory =
  144. JSDURI.prototype.fileName  =
  145. JSDURI.prototype.username  =
  146. JSDURI.prototype.password  =
  147. JSDURI.prototype.hostPort  =
  148. JSDURI.prototype.path      =
  149. JSDURI.prototype.asciiHost =
  150. JSDURI.prototype.userPass  = "";
  151.  
  152. JSDURI.prototype.port = JSD_DEFAULT_PORT;
  153.  
  154. JSDURI.prototype.schemeIs =
  155. function jsduri_schemeis (scheme)
  156. {
  157.     return scheme.toLowerCase() == "x-jsd";
  158. }
  159.  
  160. JSDURI.prototype.getCommonBaseSpec =
  161. function jsduri_commonbase (uri)
  162. {
  163.     return "x-jsd:";
  164. }
  165.  
  166. JSDURI.prototype.getRelativeSpec =
  167. function jsduri_commonbase (uri)
  168. {
  169.     return uri;
  170. }
  171.  
  172. JSDURI.prototype.equals =
  173. function jsduri_equals (uri)
  174. {
  175.     return uri.spec == this.spec;
  176. }
  177.  
  178. JSDURI.prototype.clone =
  179. function jsduri_clone ()
  180. {
  181.     return new JSDURI (this.spec);
  182. }
  183.  
  184. JSDURI.prototype.resolve =
  185. function jsduri_resolve(path)
  186. {
  187.     //dump ("resolve " + path + " from " + this.spec + "\n");
  188.     if (path[0] == "#")
  189.         return this.spec + path;
  190.     
  191.     return path;
  192. }
  193.  
  194. function JSDProtocolHandler()
  195. {
  196.     /* nothing here */
  197. }
  198.  
  199. JSDProtocolHandler.prototype.scheme = "x-jsd";
  200. JSDProtocolHandler.prototype.defaultPort = JSD_DEFAULT_PORT;
  201. JSDProtocolHandler.prototype.protocolFlags = nsIProtocolHandler.URI_NORELATIVE;
  202.  
  203. JSDProtocolHandler.prototype.allowPort =
  204. function jsdph_allowport (aPort, aScheme)
  205. {
  206.     return false;
  207. }
  208.  
  209. JSDProtocolHandler.prototype.newURI =
  210. function jsdph_newuri (spec, charset, baseURI)
  211. {
  212.     if (baseURI)
  213.     {
  214.         debug ("-*- jsdHandler: aBaseURI passed to newURI, bailing.\n");
  215.         return null;
  216.     }
  217.  
  218.     var clazz = Components.classes[SIMPLEURI_CTRID];
  219.     var uri = clazz.createInstance(nsIURI);
  220.     uri.spec = spec;
  221.     return uri;
  222. }
  223.  
  224. JSDProtocolHandler.prototype.newChannel =
  225. function jsdph_newchannel (uri)
  226. {
  227.     return new JSDChannel (uri);
  228. }
  229.  
  230. function JSDChannel (uri)
  231. {
  232.     this.URI = uri;
  233.     this.originalURI = uri;
  234.     this._isPending = true;
  235.     var clazz = Components.classes[STRING_STREAM_CTRID];
  236.     this.stringStream = clazz.createInstance(nsIStringInputStream);
  237. }
  238.  
  239. JSDChannel.prototype.QueryInterface =
  240. function jsdch_qi (iid)
  241. {
  242.  
  243.     if (!iid.equals(nsIChannel) && !iid.equals(nsIRequest) &&
  244.         !iid.equals(nsISupports))
  245.         throw Components.results.NS_ERROR_NO_INTERFACE;
  246.  
  247.     return this;
  248. }
  249.  
  250. /* nsIChannel */
  251. JSDChannel.prototype.loadAttributes = null;
  252. JSDChannel.prototype.contentType = "text/html";
  253. JSDChannel.prototype.contentLength = -1;
  254. JSDChannel.prototype.owner = null;
  255. JSDChannel.prototype.loadGroup = null;
  256. JSDChannel.prototype.notificationCallbacks = null;
  257. JSDChannel.prototype.securityInfo = null;
  258.  
  259. JSDChannel.prototype.open =
  260. function jsdch_open()
  261. {
  262.     throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  263. }
  264.  
  265. JSDChannel.prototype.asyncOpen =
  266. function jsdch_aopen (streamListener, context)
  267. {
  268.     this.streamListener = streamListener;
  269.     this.context = context;
  270.     if (this.loadGroup)
  271.         this.loadGroup.addRequest (this, null);
  272.  
  273.     var window = findDebuggerWindow();
  274.     var ary = this.URI.spec.match (/x-jsd:([^:]+)/);
  275.     var exception;
  276.     
  277.     if (window && "console" in window && ary)
  278.     {
  279.         try
  280.         {
  281.             window.asyncOpenJSDURL (this, streamListener, context);
  282.             return;
  283.         }
  284.         catch (ex)
  285.         {
  286.             exception = ex;
  287.         }
  288.     }
  289.     
  290.     var str =
  291.         "<html><head><title>Error</title></head><body>Could not load <<b>" +
  292.         this.URI.spec + "</b>><br>";
  293.     
  294.     if (!ary)
  295.     {
  296.         str += "<b>Error parsing uri.</b>";
  297.     }
  298.     else if (exception)
  299.     {
  300.         str += "<b>Internal error: " + exception + "</b><br><pre>" + 
  301.             exception.stack;
  302.     }
  303.     else
  304.     {
  305.         str += "<b>Debugger is not running.</b>";
  306.     }
  307.     
  308.     str += "</body></html>";
  309.     
  310.     this.respond (str);
  311. }
  312.  
  313. JSDChannel.prototype.respond =
  314. function jsdch_respond (str)
  315. {
  316.     this.streamListener.onStartRequest (this, this.context);
  317.  
  318.     var len = str.length;
  319.     this.stringStream.setData (str, len);
  320.     this.streamListener.onDataAvailable (this, this.context,
  321.                                          this.stringStream, 0, len);
  322.     this.streamListener.onStopRequest (this, this.context,
  323.                                        Components.results.NS_OK);
  324.     if (this.loadGroup)
  325.         this.loadGroup.removeRequest (this, null, Components.results.NS_OK);
  326.     this._isPending = false;    
  327. }
  328.  
  329. /* nsIRequest */
  330. JSDChannel.prototype.isPending =
  331. function jsdch_ispending ()
  332. {
  333.     return this._isPending;
  334. }
  335.  
  336. JSDChannel.prototype.status = Components.results.NS_OK;
  337.  
  338. JSDChannel.prototype.cancel =
  339. function jsdch_cancel (status)
  340. {
  341.     if (this._isPending)
  342.     {
  343.         this._isPending = false;
  344.         this.streamListener.onStopRequest (this, this.context, status);
  345.         if (this.loadGroup)
  346.         {
  347.             try
  348.             {
  349.                 this.loadGroup.removeRequest (this, null, status);
  350.             }
  351.             catch (ex)
  352.             {
  353.                 debug ("we're not in the load group?\n");
  354.             }
  355.         }
  356.     }
  357.     
  358.     this.status = status;
  359. }
  360.  
  361. JSDChannel.prototype.suspend =
  362. JSDChannel.prototype.resume =
  363. function jsdch_notimpl ()
  364. {
  365.     throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  366. }
  367.  
  368. /*****************************************************************************/
  369.  
  370. var Module = new Object();
  371.  
  372. Module.registerSelf =
  373. function (compMgr, fileSpec, location, type)
  374. {
  375.     debug("*** Registering -venkman handler.\n");
  376.     
  377.     compMgr =
  378.         compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  379.  
  380.     compMgr.registerFactoryLocation(CLINE_SERVICE_CID,
  381.                                     "Venkman CommandLine Service",
  382.                                     CLINE_SERVICE_CTRID, 
  383.                                     fileSpec,
  384.                                     location, 
  385.                                     type);
  386.  
  387.     catman = Components.classes[CATMAN_CTRID].getService(nsICategoryManager);
  388.     catman.addCategoryEntry("command-line-argument-handlers",
  389.                             "venkman command line handler",
  390.                             CLINE_SERVICE_CTRID, true, true);
  391.  
  392.     debug("*** Registering x-jsd protocol handler.\n");
  393.     compMgr.registerFactoryLocation(JSDPROT_HANDLER_CID,
  394.                                     "x-jsd protocol handler",
  395.                                     JSDPROT_HANDLER_CTRID, 
  396.                                     fileSpec, 
  397.                                     location,
  398.                                     type);
  399.     try
  400.     {
  401.         const JSD_CTRID = "@mozilla.org/js/jsd/debugger-service;1";
  402.         const jsdIDebuggerService = Components.interfaces.jsdIDebuggerService;
  403.         var jsds = Components.classes[JSD_CTRID].getService(jsdIDebuggerService);
  404.         jsds.initAtStartup = true;
  405.     }
  406.     catch (ex)
  407.     {
  408.         debug ("*** ERROR initializing debugger service");
  409.         debug (ex);
  410.     }
  411. }
  412.  
  413. Module.unregisterSelf =
  414. function(compMgr, fileSpec, location)
  415. {
  416.     compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  417.  
  418.     compMgr.unregisterFactoryLocation(CLINE_SERVICE_CID, fileSpec);
  419.     catman = Components.classes[CATMAN_CTRID].getService(nsICategoryManager);
  420.     catman.deleteCategoryEntry("command-line-argument-handlers",
  421.                                CLINE_SERVICE_CTRID, true);
  422. }
  423.  
  424. Module.getClassObject =
  425. function (compMgr, cid, iid) {
  426.     if (cid.equals(CLINE_SERVICE_CID))
  427.         return CLineFactory;
  428.  
  429.     if (cid.equals(JSDPROT_HANDLER_CID))
  430.         return JSDProtocolHandlerFactory;
  431.     
  432.     if (!iid.equals(Components.interfaces.nsIFactory))
  433.         throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  434.  
  435.     throw Components.results.NS_ERROR_NO_INTERFACE;
  436.     
  437. }
  438.  
  439. Module.canUnload =
  440. function(compMgr)
  441. {
  442.     return true;
  443. }
  444.  
  445. /* entrypoint */
  446. function NSGetModule(compMgr, fileSpec) {
  447.     return Module;
  448. }
  449.